<template>
	<view>  <!-- @click="openAuth('WRITE_EXTERNAL_STORAGE')" -->
		<u-form-item :required="item.required" :label="$t(item.label)" :key="item.key">
			<view>
				<view class="filePreview">
					<view class="item" v-if="filesLoading && videoName !== ''">
						<view class="text">
							{{ videoName.substring(videoName.length, videoName.length - 20) }}
						</view>
						<view class="delete" @click="deleteUploadList()">X</view>
					</view>
				</view>
				<view class="addFiles" @click="chooseFiles">{{ $t('点击上传视频') }}</view>
			</view>
		</u-form-item>
		
		<!-- <yk-authpup ref="authpup" type="top" @changeAuth="changeAuth" :permissionID="permissionID"></yk-authpup> -->
		
		<u-select :confirm-text="$t('确定')" :cancel-text="$t('取消')" v-model="choose_show" :list="chooseList" @confirm="chooseAvatar"></u-select>
		<!-- <yk-authpup ref="authpup1" type="top" @changeAuth="changeAuth1" :permissionID="permissionID1"></yk-authpup>
		<yk-authpup ref="authpup2" type="top" @changeAuth="changeAuth2" :permissionID="permissionID2"></yk-authpup> -->
		
		
	</view>
</template>
<script>
	import ykAuthpup from "@/components/yk-authpup/yk-authpup";
	
	import {
		HTTP_REQUEST_URL,
		FILE_MAX_SIZE,
		FILE_MAX_COUNT
	} from '@/config/app';
	var shuzu = [];
	export default {
		components:{
			ykAuthpup 
		},
		props: {
			item: {
				type: Object,
				default: {},
				required: true
			}
		},
		data() {
			return {
				uploadDataList: [],
				action: HTTP_REQUEST_URL + '/common/upload',
				width: 150,
				height: 150,
				filesLoading: false,
				videoName: '',
				
				plat:uni.getStorageSync('plat'),
				
				platform:'',
				permissionID: 'WRITE_EXTERNAL_STORAGE',
				permisesions: [
					{
						key: "WRITE_EXTERNAL_STORAGE",
						value: false
					},{
						key: "CAMERA",
						value: false,
					}
				],
				
				permissionID1: 'CAMERA',
				permissionID2: 'WRITE_EXTERNAL_STORAGE',
				
				choose_show: false,
				chooseList:[
					{
						value: '1',
						label: this.$t('拍摄')
					},
					{
						value: '2',
						label: this.$t('从相册选择')
					},
						
				],
				
			};
		},
		mounted() {
			
			console.log('this.item.value-videos', this.item)
			if (this.item.value == null) {
				this.item.value = []
			}
			this.uploadDataList = JSON.parse(JSON.stringify(this.item.value))
			console.log('this.item.value-uploadDataList', this.uploadDataList)
			if (this.item.value.length > 0) {
				this.videoName = this.item.value[0].url
			}
			//如果父组件传值为字符串-按照UI组件渲染标准格式化传递参数
			if (typeof this.item.value === 'string') {
				this.videoName = this.item.value;
			}
			this.filesLoading = true
			
			const systemInfo = uni.getSystemInfoSync();
			const platform = systemInfo.platform;
			this.platform = platform
			if (platform != 'ios') {
				this.requestAndroidPermission('camera')
				this.requestAndroidPermission('photoLibrary')
			}
		},
		methods: {
			async requestAndroidPermission(id) {
				var that = this
				var result = await permision.requestAndroidPermission(id)
				console.log(id,result)
				// if(result!=0){
				// 	for(var i=0;i<that.permisesions.length;i++){
				// 		if(that.permisesions[i].key=='CAMERA'){
				// 			that.permisesions[i].value = true
				// 		}
				// 	}
				// }
			},
			async requestAndroidPermission2(id) {
				var that = this
				var result = await permision.requestAndroidPermission(id)
				console.log(id,result)
				// if(result!=0){
				// 	for(var i=0;i<that.permisesions.length;i++){
				// 		if(that.permisesions[i].key=='WRITE_EXTERNAL_STORAGE'){
				// 			that.permisesions[i].value = true
				// 		}
				// 	}
				// }
			},
			//打开自定义权限目的弹框
			openAuth(permissionID='') {
				//this.openAuth('CAMERA')
				//到数组中找到没授权的权限
				if (this.platform != 'ios') {
					const not = this.permisesions.find(item => item.value === false);
					if(not){
						this.permissionID = not.key;
						setTimeout(() => {
							this.$refs['authpup'].open();
						}, 500)
					}
				}
				//这个是对应的权限 ACCESS_FINE_LOCATION 位置权限 / WRITE_EXTERNAL_STORAGE 存储空间/照片权限 / CAMERA相机权限 / CALL_PHONE 拨打电话
			},
			//用户授权权限后的回调
			changeAuth() {
				const currentPermission = this.permisesions.find(item => item.key === this.permissionID);
				currentPermission.value = true
				//这里是权限通过后执行自己的代码逻辑
				console.log('权限已授权，可执行自己的代码逻辑了');
				this.openAuth()
			},
			districts(val) {
				return val.length > 6 ? val.slice(0, 6) + "..." : val
			},
			onSuccess(data, index, lists, name) { // 上传成功
				this.setFromData(lists)
			},
			onRemove(index, lists, name) {
				// console.log('remove', lists)
				this.setFromData(lists)
			},
			setFromData(lists) {
				let formData = [];
				if (lists.length > 0) {
					for (let i = 0; i < lists.length; i++) {
						if (lists[i].response) {
							let temp = {
								url: lists[i].response.data.url
							}
							formData.push(temp)
						} else if (lists[i].url) {
							let temp = {
								url: lists[i].url
							}
							formData.push(temp)
						}
					}
				} else {
					formData = []
				}
				// console.log('formData', formData)
				this.$emit('change', this.item.key, formData)
			},
			//历史上传函数
			chooseFiles_bak() {
				const that = this
				uni.chooseVideo({
					count: 1,
					compressed: true,
					success: function(res) {
						uni.showLoading({
							title: that.$t('上传中')
						})
						if (res.size > 52428800) {
							that.$u.toast(that.$t('上传文件过大'))
							return false
						}
						console.log('chooseVideo', res)
						const tempFilePaths = res.tempFilePath
						that.videoName = res.tempFilePath
						that.uploadFile(tempFilePaths)
					}
				});
			},
			chooseAvatar(e) {
				console.log('chooseAvatar',e);
				let chooseImagetype=''
				if(e[0].value=='1')chooseImagetype='camera'
				if(e[0].value=='2')chooseImagetype='album'
				const that = this
				that.chooseImagetype = chooseImagetype
				// if(e[0].value=='1'){
				// 	if (this.platform != 'ios') {	
				// 		this.$refs['authpup1'].open();
				// 	}
				// }
				// if(e[0].value=='2'){
				// 	if (this.platform != 'ios') {	
				// 		this.$refs['authpup2'].open();
				// 	}
				// }
			},
			
			chooseFiles() {
				if (this.platform != 'ios') {
					this.choose_show=true
				}else{
					this.chooseFiles3([this.$t("拍照"), this.$t("从相册选择")])
				}
			},
			chooseFiles2(arr) {
				const that = this 
				// uni.showActionSheet({
				// 	itemList: arr,
				// 	success(e) {
						uni.chooseVideo({
							// sourceType: [e.tapIndex === 0 ? "camera" : "album"],
							sourceType:arr,
							count: 1,
							compressed: true,
							success: function(res) {
								uni.showLoading({
									title: that.$t('上传中')
								})
								if (res.size > 52428800) {
									that.$u.toast(that.$t('上传文件过大'))
									return false
								}
								console.log('chooseVideo', res)
								const tempFilePaths = res.tempFilePath
								that.videoName = res.tempFilePath
								that.uploadFile(tempFilePaths)
							}
						});
			
					//}
				//})
			},
			chooseFiles3(arr) {
				const that = this 
				uni.showActionSheet({
					itemList: arr,
					success(e) {
						uni.chooseVideo({
							sourceType: [e.tapIndex === 0 ? "camera" : "album"],
							count: 1,
							compressed: true,
							success: function(res) {
								uni.showLoading({
									title: that.$t('上传中')
								})
								if (res.size > 52428800) {
									that.$u.toast(that.$t('上传文件过大'))
									return false
								}
								console.log('chooseVideo', res)
								const tempFilePaths = res.tempFilePath
								that.videoName = res.tempFilePath
								that.uploadFile(tempFilePaths)
							}
						});
			
					}
				})
			},
			changeAuth1() {
				console.log('拍摄权限已授权，可执行自己的代码逻辑了');
				this.chooseFiles2(["camera"])
			},
			
			changeAuth2() {
				console.log('相册权限已授权，可执行自己的代码逻辑了');
				this.chooseFiles2(["album"])
			},
			uploadFile(file) {
				const that = this
				shuzu = [];
				uni.uploadFile({
					url: that.action,
					filePath: file,
					name: 'file',
					success: (uploadFileRes) => {
						console.log('uploadFileRes', uploadFileRes)
						uni.hideLoading()
						const resData = JSON.parse(uploadFileRes.data)
						if (resData.code === 1) {
							let resUrl = {
								url: resData.data.url
							}
							shuzu.push(resUrl)
							that.uploadDataList = shuzu
							that.$emit('change', that.item.key, that.uploadDataList)
						} else {
							that.$u.toast(resData.msg)
						}
					}
				});
			},
			deleteUploadList() {
				this.uploadDataList.splice(0, 1)
				this.videoName = ''
				this.$emit('change', this.item.key, this.uploadDataList)
			}
		}
	}
</script>

<style lang="scss">
	.addFiles {
		color: #F79B29;
	}

	.filePreview {
		display: flex;
		flex-direction: column;

		.item {
			display: flex;

			.text {
				color: #999999;
				width: 360rpx;
				overflow: hidden;
				word-wrap: break-word;
				word-break: break-all;
				white-space: normal;
			}

			.delete {
				font-size: 20rpx;
				width: 30rpx;
				height: 30rpx;
				background-color: #fa3534;
				color: #FFFFFF;
				border-radius: 50%;
				line-height: 30rpx;
				text-align: center;
			}
		}
	}
</style>